// 封装axios ===> 发ajax
let axios = require("axios");
let Qs = require("qs");

axios.defaults.baseURL = "http://47.96.154.185:3701";
function get(url, params, callback) {
  axios
    .get(url, { params })
    .then((response) => {
      callback(response.data);
    })
    .catch((error) => {
      callback(error);
    });
}
function post(url, params, callback) {
  axios
    .post(url, Qs.stringify(params))
    .then((response) => {
      callback(response.data);
    })
    .catch((error) => {
      callback(error);
    });
}
//请求拦截器
axios.interceptors.request.use(function (config) {
  let token = localStorage.getItem('tk');
  if (token) {
    config.headers.Authorization = 'Bearer ' + token;
  } else {
    console.log('你不行')
  }

  return config
}, function (error) {
  return Promise.reject(error)
})

// 设置头像
function upLoadPic(url,data,callbakc) {
  axios.post(url,data).then(function (res) {
    callbakc(res.data)
  }).catch(function (err) {
    callbakc(err.data)
  })
}
// 4.暴露对象
module.exports = {
  get,
  post,
  upLoadPic
};
